﻿using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAO;

namespace BUS
{
    public class HangGiaCong_BUS
    {

        private static HangGiaCong_BUS m_instance = null;

        public static HangGiaCong_BUS Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new HangGiaCong_BUS();
                }
                return m_instance;
            }
        }

        /// <summary>
        ///   lay danh sach hang gia cong
        /// </summary>
        /// 
        public List<HangGiaCong_DTO> LayDanhSachHangGiaCong()
        {
            List<HangGiaCong_DTO> result = new List<HangGiaCong_DTO>();

            DataTable dt = new DataTable();
            dt = HangGiaCong_DAO.Instance.LayToanBoHangGiaCong();

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return result;
            }

            for (int i = 0; i < list.Count; i++)
            {
                DataRow dr = list[i];

                string _id = (string)dr["MaHangGC"];
                string _name = (string)dr["TenHangGC"];
                decimal _temp = (decimal)dr["DonGia"];
                long _price = (long)_temp;

                HangGiaCong_DTO item = new HangGiaCong_DTO(_id, _name, _price);
                result.Add(item);
            }
            return result;
        }

        /// <summary>
        ///   lay toan bo ma hang gia cong
        ///   
        /// </summary>
        /// 
        public List<string> LayToanBoMaHangGiaCong()
        {
            List<string> result = new List<string>();
            DataTable dt = HangGiaCong_DAO.Instance.LayToanBoHangGiaCong();
            if (dt == null)
                return result;
            DataRowCollection list = dt.Rows;
            if (list == null)
            {
                return null;
            }
            for (int i = 0; i < list.Count; i++)
            {
                DataRow dr = list[i];
                string _id   = (string)dr["MaHangGC"];
                result.Add(_id);
            }
            return result;
        }


        /// <summary>
        ///   lay thong tin hang gia cong bang id
        /// </summary>
        /// 
        public HangGiaCong_DTO LayThongTinHangGiaCong(string _id)
        {
            HangGiaCong_DTO result;
            DataTable dt = HangGiaCong_DAO.Instance.LayThongTinHangGiaCong(_id);
            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return null;
            }
            DataRow dr = list[0];
            string _name = (string)dr["TenHangGC"];
            decimal _temp = (decimal)dr["DonGia"];
            long _dongia = (long)_temp;
            result = new HangGiaCong_DTO(_id, _name, _dongia);
            return result;
        }


        /// <summary>
        ///   Them hang gia cong moi
        /// </summary>
        /// 

        public bool ThemHangGiaCongMoi(HangGiaCong_DTO item)
        {
            return HangGiaCong_DAO.Instance.ThemHangGiaCongMoi(item.TenHangGC, item.DonGia);
        }

        /// <summary>
        ///   sua hang gia cong 
        /// </summary>
        /// 
        public bool SuaHangGiaCong(HangGiaCong_DTO item)
        {
            return HangGiaCong_DAO.Instance.SuaHangGiaCong(item.MaHangGC,item.TenHangGC,item.DonGia);
        }

        /// <summary>
        ///   xoa hang gia cong
        /// </summary>
        /// 
        public bool XoaHangGiaCong(HangGiaCong_DTO item)
        {
            return HangGiaCong_DAO.Instance.XoaHangGiaCong(item.MaHangGC);
        }

    }
}
